package com.example.myapplication.flutter

import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
import android.os.Bundle
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.myapplication.R
import com.example.myapplication.databinding.FlutterActivityBinding
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.EventChannel.EventSink
import io.flutter.plugin.common.MethodChannel.Result


class Flutter0930Activity : FlutterActivity() {
    lateinit var methodChannel: MethodChannel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var binding =
            DataBindingUtil.setContentView<FlutterActivityBinding>(this, R.layout.flutter_activity)
        binding.button3.setOnClickListener {
            startActivity(FlutterActivity.createDefaultIntent(this))
//            startActivity(FlutterActivity.withNewEngine().initialRoute("/DEmo").build(this))
        }
        binding.button4.setOnClickListener {
            startActivity(FlutterActivity.withCachedEngine("my_engine_id").build(this))
        }
        binding.button5.setOnClickListener {
//            startActivity(FlutterActivity.withNewEngine().initialRoute("MyHomePage?{'key':'value','key2':'value2'}").build(this))


            EventChannel(
                flutterEngine!!.dartExecutor.binaryMessenger,
                "Event_Channel"
            ).setStreamHandler(object :
                EventChannel.StreamHandler {
                override fun onListen(o: Any, eventSink: EventSink) {
                    eventSink.success("EventChannel：用于数据流的通信，持续通信，收到消息后无法回复此次消息。通常用于Native向Dart的通信。")
                }

                override fun onCancel(o: Any) {}
            })
        }
        var flutterView = binding.flutterView
        flutterView.attachToFlutterEngine(flutterEngine!!)

        binding.button6.setOnClickListener {
            methodChannel.invokeMethod("getPlatform", null, object : MethodChannel.Result {
                override fun success(result: Any?) {
                    binding.textView13.setText(result.toString())
                }

                override fun error(errorCode: String?, errorMessage: String?, errorDetails: Any?) {
                    binding.textView13.setText("error")

                }

                override fun notImplemented() {
                    binding.textView13.setText("notImplemented")

                }
            })

        }
        methodChannel.setMethodCallHandler { call, result ->
            if (call.method.equals("getBatteryLevel")) {
                val batteryLevel: Int = 100
                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null)
                }

            }


        }
    }


    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        //初始化MethodChannel
        methodChannel = MethodChannel(
            flutterEngine!!.dartExecutor.binaryMessenger,
            "methodChannel"
        );




    }


}